﻿function Uploader()
{
	var __uploadId = null;
	var __xmlHttp = null;
	var __status = '';
	var __params = '';
	var __nameBySystem = true;
	var __overwrite = true;
	var __interval = 200;
	var __onChange = null;
	var __onComplete = null;
	var __webPath = '/';
	var __info = null;
	var __forms = [];
	
	this.GetInfo = function(){return __info;};

	this.Init = function(option)
	{
		if (option.Status != null)
			__status = option.Status;
		if (option.Params != null)
			__params = option.Params;
		if (option.NameBySystem != null)
			__nameBySystem = option.NameBySystem;
		if (option.Overwrite != null)
			__overwrite = option.Overwrite;
		if (option.Interval != null)
			__interval = option.Interval;
		if (option.OnChange != null)
			__onChange = option.OnChange;
		if (option.OnComplete != null)
			__onComplete = option.OnComplete;
		if (option.WebPath != null)
			__webPath = option.WebPath;
	};
	
	this.OnChange = function()
	{
		for (var i = 0; i < __forms.length; i++)
		{
			if (__forms[i].fileId == this.id)
			{
				if (__forms[i].textboxId)
				{
					var textbox = document.getElementById(__forms[i].textboxId);
					textbox.value = this.value;
					$(textbox).change();
				}

				if (__forms[i].onChange)
					__forms[i].onChange(__forms[i].name);

				if (__onChange != null)
					__onChange();
			}
		}
	};
	
	this.Add = function(name, textboxId, browseId, onChange)
	{
		if (name == '' || (textboxId && document.getElementById(textboxId) == null) || document.getElementById(browseId) == null)
			return;
			
		for (var i = 0; i < __forms.length; i++)
		{
			if (__forms[i].name == name)
				return;
		}
		
		var form = {name:name,id:'Uploader_Form_' + name,fileId:'Uploader_File_' + name,textboxId:textboxId,browseId:browseId,onChange:onChange};
		
		var browseElement = document.getElementById(browseId);

		//获取左上角
		var doc = browseElement && browseElement.ownerDocument;
		var docElem = doc.documentElement;
		var win = (doc != null && doc === doc.window) ? doc : doc.nodeType === 9 && doc.defaultView;
		var top = browseElement.getBoundingClientRect().top + win.pageYOffset + docElem.clientTop;
		var left = browseElement.getBoundingClientRect().left + win.pageXOffset + docElem.clientLeft;

		//获取宽度和高度
		var w = browseElement.offsetWidth + 'px';
		var h = browseElement.offsetHeight + 'px';
		
		var formElement = null;
		if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.match(/MSIE\s*(9|10)/) == null)
		{
			formElement = document.createElement("<form method='post' enctype='multipart/form-data'></form>");
			//formElement.style.filter = "alpha(opacity = 0)";
		}
		else
		{
			formElement = document.createElement("FORM");
			formElement.method = "post";
			formElement.enctype = "multipart/form-data";
			//formElement.style.opacity = 0;
		}
		formElement.id = form.id;
		//formElement.style.display = "none";
		formElement.style.position = 'absolute';
		formElement.style.left = left + 'px';
		formElement.style.top = top + 'px';
		formElement.style.margin = "0";
		formElement.style.padding = "0";
		formElement.style.width = w;
		formElement.style.height = h;
		formElement.style.overflow = "hidden";
		document.body.insertBefore(formElement, null);
		
		var wraper = document.createElement('a');
		wraper.href = 'javascript:;';
		wraper.style.display = 'inline-block';
		wraper.style.width = w;
		wraper.style.height = h;
		wraper.style.overflow = 'hidden';
		formElement.insertBefore(wraper, null);

		var fileElement = document.createElement("INPUT");
		fileElement.type = 'file';
		fileElement.name = name;
		fileElement.id = form.fileId;
		//fileElement.size = "1";
		//fileElement.style.marginLeft = "-25px";
		fileElement.style.opacity = 0;
		fileElement.style.width = w;
		fileElement.style.height = h;
		//fileElement.style.fontFamily = "宋体";
		//fileElement.style.fontSize = "12px";
		fileElement.onchange = this.OnChange;
		wraper.insertBefore(fileElement, null);
		
		var submitElement = document.createElement("INPUT");
		submitElement.type = "submit";
		submitElement.setAttribute("id", "Submit");
		submitElement.style.display = "none";
		formElement.insertBefore(submitElement, null);
		
		__forms[__forms.length] = form;
	};
	
	this.Delete = function(name)
	{
		var found = false;
		for (var i = 0; i < __forms.length; i++)
		{
			if (found)
			{
				__forms[i - 1] = __forms[i];
			}
			else if (__forms[i].name == name)
			{
				document.body.removeChild(document.getElementById(__forms[i].id));
				found = true;
			}
		}
		
		if (found)
			__forms.length -= 1;
	};
	
	this.NeedUpload = function(name)
	{
		for (var i = 0; i < __forms.length; i++)
		{
			if (__forms[i].name == name)
			{
				var value = document.getElementById(__forms[i].fileId).value;
				if (value != ''/* && value == document.getElementById(__forms[i].TextboxID).value*/)
					return true;
				else
					return false;
			}
		}
		
		return false;
	};
	
	this.XMLStateChange = function()
	{
		if (__xmlHttp.readyState == 4)
		{
			if (__xmlHttp.status == 200)
			{
				if (__uploadId == null)
				{
					window.status = "";
					return;
				}
				
				__info = eval(__xmlHttp.responseText);
	
				//if (parseFloat(__info.total) > 0)
				//	__info.percent = (parseFloat(__info.process) * 100 / parseFloat(__info.total)).toFixed(2) + "%";
				//else
				//	__info.percent = "";

				//if (typeof(SetStatusMessage) != "undefined")
				//	SetStatusMessage(__status + __info.percent, null, 10000000);
				//else
				//	window.status = __status + __info.percent;

				//if (__onChange != null)
				//	__onChange();
				
				setTimeout("_uploader.ProgressCallback()", __interval);
			}
		}
	};
	
	this.ProgressCallback = function()
	{
		if (__uploadId == null)
		{
			window.status = "";
			return;
		}
		
		if (window.ActiveXObject)
			__xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		else
			__xmlHttp = new XMLHttpRequest();
		__xmlHttp.onreadystatechange = this.XMLStateChange;
		__xmlHttp.open("GET", __webPath + "alicms.aspx?Action=UploadState&UploadID=" + __uploadId, true);
		__xmlHttp.send("");
	};
	
	this.CompleteCallback = function(json)
	{
		if (__uploadId != null)
		{
			//clearTimeout(__uploadId);
			__uploadId = null;
		}
		
		if (__onComplete != null)
		{
			__onComplete(json);
		}
		else
		{
			if (json.error != 0)
				alert(unescape(json.message));
		}
		
		__info = null;
	};
	
	this.Upload = function(name)
	{
		if (name == null || !this.NeedUpload(name) || __uploadId != null)
			return;
		
		if (arguments.length == 2)
			__onComplete = arguments[1];
		
		var form = null;
		for (var i = 0; i < __forms.length; i++)
		{
			if (__forms[i].name == name)
			{
				form = document.getElementById(__forms[i].id);
				break;
			}
		}
		
		if (form == null)
			return;
		
		var frame = document.getElementById("Uploader_Frame");
		if (frame == null)
		{
			if(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.match(/MSIE\s*(9|10)/) == null)
			{
				frame = document.createElement("<iframe name=\"Uploader_Frame\" id=\"Uploader_Frame\" width=\"0\" height=\"0\" style=\"display:none\"></iframe>");
			}
			else
			{
				frame = document.createElement("iframe");
				frame.name = "Uploader_Frame";
				frame.id = "Uploader_Frame";
				frame.width = "0";
				frame.height = "0";
				frame.style.display = "none";
			}
			document.body.insertBefore(frame, null);
		}
		
		//__uploadId = setTimeout("_uploader.ProgressCallback()", __interval);
		__uploadId = Math.random();
		form.action = __webPath + "alicms.aspx?Action=Upload&UploadID=" + __uploadId + "&UploadFileNameBySystem= " + __nameBySystem + "&UploadFileOverwrite=" + __overwrite + "&Callback=parent._uploader.CompleteCallback&" + __params;
		form.target = "Uploader_Frame";
		form.Submit.click();
	};
}
var _uploader = new Uploader();